package com.neusoft.utils.common.dao.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.utils.common.dao.IBaseDao;
import com.neusoft.utils.page.PageFactory;
import com.vonchange.headb.core.bean.SqlParmeter;
import com.vonchange.headb.core.map.HeaMap;
import com.vonchange.headb.spring.dao.impl.HeaDaoImpl;


public abstract class BaseDaoImpl<T> extends   HeaDaoImpl<T>  implements IBaseDao<T>{
	/**
	 * 分页查询(返回spring的分页实体)
	 * @param page
	 * @param parameter
	 * @return IPage<T>
	 */
  	@Override
	public final Page<T> queryPage(Pageable pageable,HeaMap parameter) {
		SqlParmeter sqlParmeter = generateQuerySql(parameter);
		String sql = sqlParmeter.getSql();
		Object[] params = sqlParmeter.getParameters();
		long totalCount = countMySqlResult(sql, params);
		int pageNo=pageable.getPageNumber();
		int pageSize=pageable.getPageSize();
		if(pageNo<0){
			pageNo=0;
		}
		int firstEntityIndex= pageNo * pageSize;
		int lastEntityIndex = (pageNo+1) * pageSize;
		sql = dialect.getPageSql(sql,firstEntityIndex, lastEntityIndex);
		List<T> entities = queryList(sql, params, sqlParmeter.getAliasMap());
		return PageFactory.createPage(entities, pageable, totalCount);
	}
}
